package cn.soft863.users.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.soft863.common.util.DbUtil;
import cn.soft863.common.util.PageInfoUtil;
import cn.soft863.users.dao.IUserExtendsDao;
import cn.soft863.users.pojo.AUserExtends;

public class UserExtendsDaoImp implements IUserExtendsDao{

	@Override
	public int insert(AUserExtends ue) throws Exception {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO a_users_extend(usersId, idCardType, idCardHeadImg, idCardBackImg, sendEmailCode, sendEmailTime, sendPhoneCode, sendPhoneTime, createTime, updateTime) VALUES "
				+ "	(?,?,?,?,?,?,?,?,?,?)"; 
		/* ����һ�����Ӷ��� */
		Connection con = DbUtil.getConnection();
		PreparedStatement ps = null ;
		ResultSet rs = null ; 
		
		int res = 0 ; 
		/* ����һ�������� */
		try
		{
			ps = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			/* ����ռλ�� */
			ps.setObject(1, ue.getUserId());
			ps.setObject(2, ue.getIdCardType());
			ps.setObject(3, ue.getIdCardHeadImg());
			ps.setObject(4, ue.getIdCardBackImg());
			ps.setObject(5, ue.getSendEmailCode());
			ps.setObject(6, ue.getSendEmailTime());
			ps.setObject(7, ue.getSendPhoneCode());
			ps.setObject(8, ue.getSendPhoneTime());
			ps.setObject(9, ue.getCreateTime());
			ps.setObject(10, ue.getUpdateTime());
			res = ps.executeUpdate();
			/* res��ʾ����sql���Ӱ������
			 * ��Ҫ��������
			 *  */
			rs = ps.getGeneratedKeys();
			/* �϶��н��,����ֻ��һ�� */
			if(rs.next())
			{
				/* ������ֵ */
				ue.setId(rs.getInt(1));
			}
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			/* �ر����� */
			DbUtil.closeAll(rs, ps, con);
		}
		return res;
	}

	@Override
	public int updateOneDao(AUserExtends ue) throws Exception {
		// TODO Auto-generated method stub
		String sql = "update a_users_extend set usersId=?, idCardType=?, idCardHeadImg=?, idCardBackImg=?, "
				+ "sendEmailCode=?, sendEmailTime=?, sendPhoneCode=?, sendPhoneTime=?, createTime=?,updateTime=? WHERE (id=?)"; 
		/* ����һ�����Ӷ��� */
		Connection con = DbUtil.getConnection();
		PreparedStatement ps = null ;
		ResultSet rs = null ; 
		
		int res = 0 ; 
		/* ����һ�������� */
		try
		{
			ps = con.prepareStatement(sql);
			/* ����ռλ�� */
			ps.setObject(1, ue.getUserId());
			ps.setObject(2, ue.getIdCardType());
			ps.setObject(3, ue.getIdCardHeadImg());
			ps.setObject(4, ue.getIdCardBackImg());
			ps.setObject(5, ue.getSendEmailCode());
			ps.setObject(6, ue.getSendEmailTime());
			ps.setObject(7, ue.getSendPhoneCode());
			ps.setObject(8, ue.getSendPhoneTime());
			ps.setObject(9, ue.getCreateTime());
			ps.setObject(10, ue.getUpdateTime());
			ps.setObject(11, ue.getId());
			res = ps.executeUpdate();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}finally
		{
			/* �ر����� */
			DbUtil.closeAll(rs, ps, con);
		}
		return res;
	}

	@Override
	public List<AUserExtends> FindByOnes(PageInfoUtil page, Map<String, Object> map) throws SQLException {
		// TODO Auto-generated method stub
		String sql="select * from a_users_extend where 1=1";
		String sql1="select count(*) from a_users_extend where 1=1";
		List<Object> list=new ArrayList<Object>();
		if(map.get("keyword") != null && !"".equalsIgnoreCase(map.get("keyword") + ""))
		{
			sql += " and (email like ? or phone like ?) ";
			sql1 += " and (email like ? or phone like ?) ";
			list.add("%" + map.get("keyword") + "%");
			list.add("%" + map.get("keyword") + "%");
			/*
			 * select * from a_admins email like ? or phone like ?
			 *  */
		}
		/* ����״̬���� */
		if(map.get("status") != null && !"".equalsIgnoreCase(map.get("status") + ""))
		{
			sql += " and status = ? ";
			sql1 += " and status = ? ";
			list.add(map.get("status"));
			/*
			 * select * from a_admins status = ?
			 *  */
		}
		/* ����ʱ������ */
		if(map.get("stDate") != null && !"".equalsIgnoreCase(map.get("edDate") + ""))
		{
			sql += " and (createTime >= ? and createTime < ? )";
			sql1 += " and (createTime >= ? and createTime < ? )";
			list.add(map.get("stDate"));
			list.add(map.get("edDate"));
			
		}
		Connection con=null;
		PreparedStatement ps=null;
		con=DbUtil.getConnection();
		ps=con.prepareStatement(sql1);
		int count = 1 ; 
		for (Iterator iterator = list.iterator(); iterator.hasNext();)
		{
			Object obj = (Object) iterator.next();
			ps.setObject(count, obj);
			count ++ ; 
		}
//		System.out.println(count);
		ResultSet rs1=ps.executeQuery();
		while(rs1.next())
		{
			page.setTotalRecord(rs1.getInt(1));
		}
		DbUtil.closeAll(rs1, ps, con);
		sql+=" limit ?,?";
		System.out.println(sql);
		con=DbUtil.getConnection();
		 ps=con.prepareStatement(sql);
		
		list.add(page.getCurrRecord());
		list.add(page.getPageSize());
		int count1 = 1 ; 
		for (Iterator iterator = list.iterator(); iterator.hasNext();)
		{
			Object obj = (Object) iterator.next();
			ps.setObject(count1, obj);
			count1 ++ ; 
		}
		
		List<AUserExtends> listUserExtends=new ArrayList<AUserExtends>();
		rs1=ps.executeQuery();
		AUserExtends userextends=new AUserExtends();
		while(rs1.next())
		{
			userextends.setId(rs1.getInt(1));
			userextends.setUserId(rs1.getInt(2));
			userextends.setIdCardType(rs1.getByte(3));
			userextends.setIdCardHeadImg(rs1.getString(4));
			userextends.setIdCardBackImg(rs1.getString(5));
			userextends.setSendEmailCode(rs1.getString(6));
			userextends.setSendEmailTime(rs1.getDate(7));
			userextends.setSendPhoneCode(rs1.getString(8));
			userextends.setSendPhoneTime(rs1.getDate(9));
			userextends.setCreateTime(rs1.getDate(10));
			userextends.setUpdateTime(rs1.getDate(11));
			
			
			
			listUserExtends.add(userextends);
		}
		DbUtil.closeAll(rs1, ps, con);
		
		return listUserExtends;
	}

}
